/**
 * @(#)ApkSimulatorServiceImpl.java 2016年11月10日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IApkSimulatorDAO;
import org.jsbd.boss.dao.ISimulatorSysVerDAO;
import org.jsbd.boss.dao.ITaskVerTimerDAO;
import org.jsbd.boss.domian.channel.*;
import org.jsbd.boss.service.IApkSimulatorService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import edu.hziee.common.tcp.cache.CachedHolder;

/**
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class ApkSimulatorServiceImpl implements IApkSimulatorService {
    public static final Logger LOGGER = LoggerFactory.getLogger(ApkSimulatorServiceImpl.class);

    @Resource
    private RedisClient redisClient;
    @Resource
    private IApkSimulatorDAO apkSimulatorDAO;

    @Resource
    private ISimulatorSysVerDAO sysDao;

    @Resource
    private CachedHolder ehcache;

    @Resource
    private ITaskVerTimerDAO taskVerTimerDAO;

    @Override
    public List<ApkSimulator> getAllApkSimulators() {
        Object apkList = redisClient.get(CacheKey.APK_VER_LIST);
        if (apkList != null) {
            return (List<ApkSimulator>) apkList;
        } else {
            ApkSimulator apkSimulator = new ApkSimulator();
            apkSimulator.setIsDelete("0");
//			apkSimulator.setDayLimit(1);
            List<ApkSimulator> list = apkSimulatorDAO.getApkSimulators(apkSimulator);
            if (list != null) {
                redisClient.put(CacheKey.APK_VER_LIST, list, 300);
            } else {
                redisClient.put(CacheKey.APK_VER_LIST, new ArrayList<ApkSimulator>(), 300);
            }
            return list;
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * org.jsbd.boss.service.IApkSimulatorService#getApkSimulators(java.lang
     * .String, java.lang.String)
     */
    @Override
    public List<ApkSimulator> getApkSimulators(String sysVer) {
        ApkSimulator apkSimulator = new ApkSimulator();
        apkSimulator.setSysVer(sysVer);
        return apkSimulatorDAO.getApkSimulators(apkSimulator);
    }

    @Override
    public void insertApkInfoList(List<ApkSimulator> apkConfigList) {
        apkSimulatorDAO.insertApkSimulator(apkConfigList);
    }

    @Override
    public int delApkSimulator(ApkSimulator apkSimulator) {
        return apkSimulatorDAO.delApkSimulator(apkSimulator);
    }

    @Override
    public List<VerInfo> selectVers(String mark, String verType) {
        List<VerInfo> resultList = new ArrayList<VerInfo>();
        List<ApkSimulator> list = apkSimulatorDAO.getVerList(mark);
        if (verType.equals("1")) {
            List<SimulatorSysVer> allSysVers = sysDao.selectSimulatorSysVer(null);
            if (allSysVers != null && allSysVers.size() > 0) {
                Set<String> verSet = new HashSet<String>();
                if (list != null && list.size() > 0) {
                    for (ApkSimulator verInfo : list) {
                        verSet.add(verInfo.getSysVer());
                    }
                }
                for (SimulatorSysVer sysVer : allSysVers) {
                    VerInfo verInfo = new VerInfo();
                    String ver = sysVer.getVer();
                    if (verSet.contains(ver)) {
                        verInfo.setSelected("true");
                    }
                    verInfo.setVer(ver);
                    resultList.add(verInfo);
                }
            }

        }
        return resultList;
    }

    @Override
    public List<VerInfo> selectVers(Long taskId) {
        List<VerInfo> resultList = new ArrayList<VerInfo>();

        TaskverTimer taskverTimer = new TaskverTimer();
        taskverTimer.setId(taskId);
        List<TaskverTimer> taskverTimers = taskVerTimerDAO.selectList(taskverTimer);
        List<SimulatorSysVer> allSysVers = sysDao.selectSimulatorSysVer(null);
        if (allSysVers != null && allSysVers.size() > 0) {
            Set<String> verSet = new HashSet<String>();
            if (taskverTimers != null && taskverTimers.size() > 0) {
                for (TaskverTimer verInfo : taskverTimers) {
                    verSet.add(verInfo.getSysver());
                }
            }
            for (SimulatorSysVer sysVer : allSysVers) {
                VerInfo verInfo = new VerInfo();
                String ver = sysVer.getVer();
                if (verSet.contains(ver)) {
                    verInfo.setSelected("true");
                }
                verInfo.setVer(ver);
                resultList.add(verInfo);
            }
        }
        return resultList;
    }

    @Override
    public List<ApkSimulator> getVers(String mark) {
        return apkSimulatorDAO.getVerList(mark);
    }

    @Override
    public List<ApkRetentConfig> selectAllRetentAPkList() {
        Object apkList = redisClient.get(CacheKey.APKR_VER_LIST);
        if (apkList != null) {
            return (List<ApkRetentConfig>) apkList;
        }
        ApkSimulator apkSimulator = new ApkSimulator();
        apkSimulator.setIsDelete("0");
        List<ApkRetentConfig> list = apkSimulatorDAO.selectAllRetentAPkList(apkSimulator);
        if (list != null) {
            redisClient.put(CacheKey.APKR_VER_LIST, list, 300);
        } else {
            redisClient.put(CacheKey.APKR_VER_LIST, new ArrayList<ApkSimulator>(), 300);
        }
        return list;
    }

    @Override
    public void updateApkVers() {
        List<ApkSimulator> list = getVers(null);
        if (list != null && list.size() > 0) {
            for (ApkSimulator apkSimulator : list) {
                String key = CacheKey.M_VER + apkSimulator.getMark();
                String ver = apkSimulator.getSysVer();
                redisClient.put(key, ver, 300);
            }
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.jsbd.boss.service.IApkSimulatorService#getVer(java.lang.String)
     */
    @Override
    public String getVer(String mark) {
        String key = CacheKey.M_VER + mark;
        Object value = ehcache.get(key);
        if (value != null) {
            return value.toString();
        }
        Object obj = redisClient.get(key);
        if (obj != null) {
            ehcache.put(key, obj.toString());
            return obj.toString();
        } else {
            return null;
        }
    }
}
